<!DOCTYPE html> 
<html lang="zh-cn"> 
<head> 
	<title>后台管理登陆</title> 
	<meta charset='utf-8' />
	<meta name="viewport" content="width=device-width, initial-scale=1.0"> 
</head> 
<body style='background:#ccc'> 
	<h1 style='text-align:center'>upyun管理登陆</h1>
	<div style='display:flex;margin:20px auto;width:250px;border:0px solid'>
		<div style=''>用户名：</div>
		<div style='flex:1'>
			<input type="text" name='user' style='width:100%' />
		</div>
	</div>
	<div style='display:flex;margin:20px auto;width:250px;border:0px solid'>
		<div style=''>密　码：</div>
		<div style='flex:1'>
			<input type="password" name='pwd'  style='width:100%' />
		</div>
	</div>
	<div style='text-align:center'>
		<button type='button' id='login' >登陆</button>
		<button type='button' id='reset' >重置</button>
	</div>
</body>
</html>

<script src="./Include/jquery.js"></script> 
<script>
	var url = 'interface.php?a='+Math.random();
	$(function(){
		if(sessionStorage.login){
			location.href='index.html';
		}
		$('#login').click(function(){

			var user = $('input[name=user]').val().trim();
			var pwd = $('input[name=pwd]').val().trim();
			if(!user || !pwd){
				alert('用户名或密码不得为空！');return;
			}
			var data = {
				type:'login',
				user:user,
				pwd:pwd
			};
			Ajax2(data,function(res){
				if(res == 'ok'){
					sessionStorage.login = 'admin';
					location.replace('index.html');
				}else{
					A('用户名或密码不正确！');
				}
			},'interface.php');
		});
	});





function Ajax2(data,fun,url){
	GetToken(data.type,url,function(t){
		data.token = t;
		data.token_name = data.type;
		Ajax(data,function(res){
			fun(res);
		},url);
	});
}
function GetToken(token_name,url,fun){
	var data = {
		type:'token',
		token_name:token_name
	}
	Ajax(data,function(t){
		fun(t);
	},url);
}
/*
	###   ajax 异步加载 (JQUERY方式)
	参一：要传送的数据
	参二：成功回调
	参三：请求的后台地址
	参四：是否异步.当无值时默认为异步，当有值时为同步
	参五：提交方式 默认为post,有值为get
	示例：
		var data = {
					token:t,
					type:type,
					openid:openid
				};

		 Ajax(data,function(res){
		 	alert(res);
		 });
		 或
		 },1);
*/
function Ajax(data,fun,url,async,stat){
	if(!stat){
		stat = 'post';
	}else{
		stat = 'get';
	}
	async = async?false:true;
	if(!url){
		url = 'interface.php';
	}
	$.ajax({
		url:url,
		type:stat,
		async:async,
		data:data,
		success(rq_res,rq_stat,rq_xhr){
			fun(rq_res,rq_stat,rq_xhr);
		},
		error(){
			fun('false');
		}
	});
}


function C(r){
	console.log(r);
}
function A(r){
	alert(r);
}

</script>